﻿using DemountCIM.Framework.DataBase;
using DemountCIM.Framework.DataBase.Table;
using DemountCIM.ViewModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfUIDLL.MessageBox;

namespace DemountCIM.Windows
{
    /// <summary>
    /// UserManagerWindow.xaml 的交互逻辑
    /// </summary>
    public partial class UserManagerWindow : Window
    {
        int userIndex;

        public UserManagerWindow()
        {
            InitializeComponent();

            this.DataContext = new UserManagerViewModel();
            this.Topmost = true;
        }



        private void UserDataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count <= 0)
                return;
            var user = e.AddedItems[0] as UserWindowModel;
            UserLevel.SelectedIndex = (int)user.Level;
            UserName.Text = user.UserName;
            UserName.Tag = user.UserId;
            PassWord.Password = user.PassWord;

            userIndex = UserDataGrid.SelectedIndex;
        }

        private void AduFlatButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var user = new TbUser
                {
                    Name = UserName.Text,
                    Pwd = PassWord.Password,
                    Level = (UserLevel)UserLevel.SelectedIndex
                };
                DataBaseContainer.GetInstance().GetUserRepository().AddUserInfo(user);

                (DataContext as UserManagerViewModel).Update();

                this.UserName.Clear();
                this.PassWord.Clear();
                this.UserLevel.SelectedIndex = -1;

            }
            catch (System.Exception ex)
            {
                Trace.WriteLine($"UserManagerPage：{ex.Message}");
                ShowMsgBox.ShowDialog("无法添加此用户，请检查用户信息", MessageType.MsgAlarm);
            }
        }

        private void AduFlatButton_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                var user = new TbUser
                {
                    Id = Convert.ToInt16(UserName.Tag),
                    Name = UserName.Text,
                    Pwd = PassWord.Password,
                    Level = (UserLevel)UserLevel.SelectedIndex
                };
                DataBaseContainer.GetInstance().GetUserRepository().ModifyUserInfo(user);

                (DataContext as UserManagerViewModel).Update();

                this.UserName.Clear();
                this.PassWord.Clear();
                this.UserLevel.SelectedIndex = -1;
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine($"UserManagerPage：{ex.Message}");
                ShowMsgBox.ShowDialog("无法修改此用户，请检查用户信息", MessageType.MsgAlarm);
            }
        }

        private void AduFlatButton_Click_2(object sender, RoutedEventArgs e)
        {
            try
            {
                var user = new TbUser
                {
                    Id = Convert.ToInt16(UserName.Tag),
                    Name = UserName.Text,
                    Pwd = PassWord.Password,
                    Level = (UserLevel)UserLevel.SelectedIndex
                };
                DataBaseContainer.GetInstance().GetUserRepository().DeleteUserInfo(user);

                (DataContext as UserManagerViewModel).Update();


                this.UserName.Clear();
                this.PassWord.Clear();
                this.UserLevel.SelectedIndex = -1;
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine($"UserManagerPage——Btn_Click_DeleteUser：{ex.Message}");
                ShowMsgBox.ShowDialog("无法删除此用户，请检查用户信息", MessageType.MsgAlarm);
            }
        }
    }
}
